# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class account_stmt(osv.osv):
    
    _inherit="account.stmt"
    
    def _get_rubylong_xml(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
#                        #公司地址
                        ('company_id.street','company_id_street'),
                        ('company_id.street2','company_id_street2'),
                        ('company_id.city','company_id_city'),
                        ('company_id.state_id.name','company_id_state'),
                        ('company_id.country_id.name','company_id_country_name'),
                        #电话,传真
                        ('company_id.phone','company_id_phone'),
                        ('company_id.fax','company_id_fax'),

                        #对账单号
                        #核算日期
                        #核算人
                        #对账公司(合作伙伴）
                        #伙伴类型
                        #总金额
                        #未支付
                        #已支付
                        'name',
                        'date_order',
                        ('user_id.name','user_id'),
                        ('partner_id.name','partner_id')
                        ]
            partner_type = selection_name(self, cr, uid, self._name, 'partner_type', order.partner_type, context=context)
            order_fields +=[
                        {'partner_type':partner_type},
                        'amount_total',
                        'amount_paid',
                        'amount_balance',
                        ('id','order_id'), 
                        ]
            #创建日期,创建人
            create_date = hasattr(order,'create_date') and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.create_date, DEFAULT_SERVER_DATETIME_FORMAT),
                                                         context=context)) or ''
            create_uid = hasattr(order,'create_uid') and order.create_uid.name or ''
            order_fields += [{'create_date':create_date},{'create_uid':create_uid}]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        #料号
                        #产品
                        #规格
                        #单位
                        #数量
                        #单价
                        #源单据
                        #关联单号
                        #送货日期(出库日期）
                        #送货单号
                        #（金额）小计
                        ('product_id.default_code','default_code'),
                        ('product_id.name','product_id'),
                        ('product_id.variants','variants'),
                        ('product_uom.name','product_uom'),
                        'product_qty',
                        ('price_stmt','price_unit'),
                        'origin',
                        ('picking_id.name','picking_id'),
                        ('picking_id.date_done','date_done'),
                        ('picking_id.deliver_ticket_no','deliver_ticket_no'),
                        ('amount','amount_total'),
                        ] 
            seq = 1
            for line in order.stmt_move_ids:      
                #销售日期
                #采购日期              
                #序号
                date_order_sale = line.picking_id.sale_id and line.picking_id.sale_id.date_order or ''
                date_order_purchase = line.picking_id.purchase_id and line.picking_id.purchase_id.date_order or ''
                line_fields += []   
                line_fields_new = line_fields + [{'order_line':seq},{'date_order_sale':date_order_sale, 'date_order_purchase':date_order_purchase}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)
                seq += 1
            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml, type='text', string='Rubylong xml'),
            }
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  